package com.higgins.eventtracking.client.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;
import com.higgins.eventtracking.client.rpc.FixedOutgoingService;
import com.higgins.eventtracking.client.rpc.FixedOutgoingServiceAsync;

public class AssetGenericTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(AssetGenericTable.class.getName());
	private final AssetTrackingServiceAsync assetTrackingService = GWT
		.create(AssetTrackingService.class);
//	private final FixedOutgoingServiceAsync fixedOutgoingService = GWT
//			.create(FixedOutgoingService.class);
	
	public AssetGenericTable(AssetGenericColumns columns)	{
		super(columns);
		retrieveDTOs();
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<AssetDTO> dtos)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (AssetDTO dto:dtos)	{
			List<Object> row = new ArrayList<Object>();
			row.add(dto.getId());
			row.add(dto.getTimestamp());
			row.add(dto.getAccount());
			row.add(dto.getAccountType());
			row.add(dto.getAmount());
			row.add(dto.getAccountRepayment());
			row.add(dto.getMonthlyRepayment());
			row.add(dto.getMonthlyInterest());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		AssetDTO dto = new AssetDTO();
		dto.setId(getId(row));
		dto.setTimestamp(getDate(row, 1));
		dto.setAccount(getText(row, 2));
		dto.setAccountType(getText(row, 3));
		dto.setAmount(getDouble(row, 4));
		dto.setAccountRepayment(getText(row, 5));
		dto.setMonthlyRepayment(getDouble(row, 6));
		dto.setMonthlyInterest(getDouble(row, 7));
		updateRow(dto);
	}
	
	protected void retrieveDTOs()	{
		try {
			assetTrackingService.retrieveAssets(new AsyncCallback<List<AssetDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<AssetDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} 
	}	
	private void updateRow(AssetDTO dto)	{
		System.out.println("updating fixedOutgoingDTO " + dto.getId());
		try {
			assetTrackingService.updateAsset(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} 	
	}
	protected void deleteRow(Long id)	{
		try {
			assetTrackingService.deleteAsset(id, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						refresh();					
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} 	
	}
}
